<template>
    <div>
        <div style="float: right">
            <el-button type="primary" v-if="numbers[0].flags==1" >已缴费</el-button>
            <el-button type="primary" @click="dialogFormVisible = true" v-if="numbers[0].flags==0"  >缴费</el-button>
        </div>
        <el-table
                :data="medecineInfo"
                border
                :summary-method="getSummaries"
                show-summary
                style="width: 100%; margin-top: 20px">
            <el-table-column
                    prop="id"
                    label="药品编号"
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="name"
                    label="名称">
            </el-table-column>
            <el-table-column
                    prop="price"
                    label="单价">
            </el-table-column>
            <el-table-column
                    prop="number"
                    label="数量">
            </el-table-column>
            <el-table-column
                    prop="total"
                    label="总价">
            </el-table-column>
        </el-table>
        <el-dialog   v-model="dialogFormVisible" width="420px" >
            <center><h2>共{{form.pay}}元 </h2></center>
            <center><img style="width: 400px;height: 400px;" src="../img/Code.jpg"></center>
            <div slot="footer"  class="dialog-footer">
                <center>
                    <el-button type="primary" @click="Submitpay">确定支付</el-button></center>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import axios from "axios";
    import {ElMessage} from "element-plus";

    export default {
        name: "MedecineList",
        data() {
            return {
                medecineInfo: [{
                    id: "",
                    name: "",
                    price: 0,
                    sort: "",
                    specification: 0,
                    vender: "",
                    batch: 0,
                    useWay: "",
                    dosage: "",
                    beginDate: "",
                    endDate: 0,
                    sell: 0,
                    gmtCreate: "",
                    gmtUpdate: "",
                    deleted: 0,
                    number: 0,
                    total:0
                }],
                numbers:[{
                    id: "",
                    number: 0,
                    doctorId: "",
                    deleted: 0,
                    date: "",
                    pid: "",
                    mid: ""
                }],
                date:'',
                id:'',
                dialogFormVisible:false,
                name:'',
                form: {
                    pay:0,
                    real_pay:'',
                    change:''
                },
            };
        },
        created(){
            this.create();
        },
        activated(){
            this.create()
        },
        methods: {
            create(){
                this.date=localStorage.getItem('date');
                this.id=localStorage.getItem("id");
                axios.post("http://119.29.60.153:8001/doctorservice/prescription/getMedecineByDate?date="+this.date+"&id="+this.id).then(resp=> {
                    this.medecineInfo=resp.data.data.medecineInfo;
                    this.numbers=resp.data.data.number;
                    for (var i=0;i<this.medecineInfo.length;i++){
                        this.medecineInfo[i].number=this.numbers[i].number;
                        this.medecineInfo[i].total= this.medecineInfo[i].price*this.medecineInfo[i].number;
                        this.form.pay+=this.medecineInfo[i].total;
                    }
                })
            },
            Submitpay(){
                axios.post("http://119.29.60.153:8001/doctorservice/prescription/payment?date="+this.date+"&id="+this.id).then(resp=> {
                    if(resp.data.message=='成功'){
                        ElMessage.success('缴费成功！');
                        this.dialogFormVisible=false;
                        this.create();
                    }
                    else ElMessage.error('缴费失败！');
                })
            },
            getSummaries(param) {
                const { columns, data } = param;
                const sums = [];
                columns.forEach((column, index) => {
                    if (index === 0) {
                        sums[index] = '总价';
                        return;
                    }
                    if (index === 1) {
                        sums[index] = '';
                        return;
                    }if (index === 2) {
                        sums[index] = '';
                        return;
                    }
                    if (index === 3) {
                        sums[index] = '';
                        return;
                    }
                    const values = data.map(item => Number(item[column.property]));
                    if (!values.every(value => isNaN(value))) {
                        sums[index] = values.reduce((prev, curr) => {
                            const value = Number(curr);
                            if (!isNaN(value)) {
                                return prev + curr;
                            } else {
                                return prev;
                            }
                        }, 0);
                        sums[index] += ' 元';
                    } else {
                        sums[index] = '';
                    }
                });

                return sums;
            }
        }
    }
</script>

<style scoped>

</style>